// =====================================================================================
// Copyright ?2006 SmartCodeGenerator created by Shahed Khan. 
/*
//Microsoft Shared Source Community License (MS-CL)
//Published: October 18, 2005
*/
// If you like this code then feel free to go ahead and use it.
// Your use of this software is entirely at your own risk.
// I make no claims or warrantees about the reliability or fitness of this code for any particular purpose.
// website www.smartcodegenerator.com, email shahed.khan@gmail.com
// =====================================================================================

using System;
using System.Collections;
using System.IO;
using System.Runtime.Serialization;


namespace SmartCodeGen.Providers.Base
{
	/// <summary>
	/// A default implementation of collection class which other classes can inherit to provide collection
	/// features.
	/// </summary>

	[Serializable]
	public abstract class BaseCollection : IEnumerable, IDisposable, IList, ISerializable
	{
		// Used to store number of items actually persisted in data source
		public int VirtualCount;

		protected ArrayList _table;

		public BaseCollection() {}

		//Deserialization constructor.
		public BaseCollection (SerializationInfo info, StreamingContext context) 
		{
			_table = (ArrayList)info.GetValue( "_table", typeof(ArrayList));
		}


		//Serialization function.
		public void GetObjectData(SerializationInfo info, StreamingContext context)
		{
			info.AddValue( "_table", _table );
		}

		protected ArrayList Table 
		{
			get
			{
				if( _table == null )
					_table = new ArrayList();

				return _table;
			}
			set
			{
				_table = value;
			}
		}

		public int Count
		{
			get
			{
				return Table.Count;
			}
		}
		public void AddRange( BaseCollection list )
		{
			Table.AddRange( list );
		}

		protected void Add( object key, object value )
		{
			//Table.Add( key, value );
			Table.Add( value );
		}

		protected object Get( int index )
		{
			//return Table.GetByIndex( index );
			return Table[ index ];
		}

		protected void Set(int index, object obj)
		{
            if (Table.Count < index+1)
            {
                Table.AddRange(new object[index+1]);
            }      
            Table[index] = obj;
		}

		protected object GetByKey( int ID )
		{
			foreach( BaseObject obj in Table )
				if( obj.ID == ID )
					return obj;

			return null;
		}

		protected object GetNextByKey( int ID )
		{			
			for( int index = 0; index< Table.Count; index++)
			{
				BaseObject obj = Table[index] as BaseObject;
				if (obj.ID == ID)
				{
					if (index+1 < Table.Count)
						return Table[index+1] as BaseObject;
					else 
						return null;
				}				
			}	
			return null;
		}

		protected object GetPreviousByKey( int ID )
		{			
			for( int index = 0; index< Table.Count; index++)
			{
				BaseObject obj = Table[index] as BaseObject;
				if (obj.ID == ID)
				{
					if (index-1 >= 0 )
						return Table[index-1] as BaseObject;
					else 
						return null;
				}				
			}	
			return null;
		}

		public virtual bool Contains( int ID )
		{
			foreach( BaseObject obj in Table )
				if( obj.ID == ID )
					return true;

			return false;
		}

		
//		
//		public virtual void Remove( object key )
//		{
//			Table.Remove( key );
//		}
//		
		public virtual void Clear( )
		{
			Table.Clear();
		}

		public virtual void Dispose()
		{
			Clear();
			Table = null;
		}

		public virtual System.Collections.IEnumerator GetEnumerator()
		{
			//return Table.Values.GetEnumerator();				
			return Table.GetEnumerator();				
		}

		public void CopyTo(System.Array array, int index)
		{
			Table.CopyTo( array, index );
		}

		public bool IsSynchronized
		{
			get
			{
				return Table.IsSynchronized;
			}
		}

		public object SyncRoot
		{
			get
			{
				return Table.SyncRoot;
			}
		}

		public int Add(object value)
		{
			return _table.Add( value );
		}

		public bool Contains(object value)
		{
			return _table.Contains( value );
		}

		public int IndexOf(object value)
		{
			return _table.IndexOf( value );
		}

		public void Insert(int index, object value)
		{
			_table.Insert( index, value );
		}



		public void Remove(object value)
		{
			_table.Remove( value );
		}

		public void RemoveAt(int index)
		{
			_table.RemoveAt( index );
		}

		public bool IsFixedSize
		{
			get
			{
				return _table.IsFixedSize;
			}
		}

		public bool IsReadOnly
		{
			get
			{
				return _table.IsReadOnly;
			}
		}

		public virtual object this[int index]
		{
			get
			{
				return _table[index];
			}
			set
			{
				_table[index] = value;
			}
		}

		public void Sort()
		{
			Table.Sort();
		}

	}
}
